package com.jimo.transadb

import android.annotation.SuppressLint
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.jimo.transadb.databinding.ActivityFrpcConfigBinding
import java.io.File
import java.io.FileWriter

class FrpcConfigActivity : AppCompatActivity() {
    private val frpcConfig: String="frpcConfig"
    private lateinit var binding: ActivityFrpcConfigBinding
    @SuppressLint("CommitPrefEdits", "SetTextI18n")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding=ActivityFrpcConfigBinding.inflate(layoutInflater)
        setContentView(binding.root)
        if(App.sp.getString(frpcConfig,"")?.isEmpty() == true){
            saveConfig("""
# 全局配置
[common]
# 服务器IP和端口
server_addr = 
server_port = 7000

# ADB服务端口的映射
[adb]
type = tcp
local_ip = 127.0.0.1
local_port = 5555
remote_port = 5555
""")
        }
        binding.configEdit.setText(App.sp.getString(frpcConfig,""))
        binding.save.setOnClickListener {
            saveConfig(binding.configEdit.text.toString())
            finish()
        }
    }
    private fun saveConfig(str:String) {
        App.sp.edit().putString(frpcConfig,str).apply()
        updateInit(str)
    }
    private fun updateInit(s: String) {
        try {
            val file = File(filesDir, "frpc.ini")
            val fileWriter = FileWriter(file)
            fileWriter.write(s)
            fileWriter.close()
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }
}